﻿using System;
using System.IO;
using System.Linq;
using Newtonsoft.Json;
using RestSharp;
using Spire.Doc;

namespace Ev_WxArticle
{
    class Program
    {
        static int totalCount = 0;

        static void Main(string[] args)
        {
            Console.WriteLine("正在读取文章列表");
            //Console.WriteLine("请粘贴文章的列表地址");
            //var strUrl = Console.ReadLine();
            //var strUrl = @"";
            //if (strUrl == null)
            //{
            //    Console.WriteLine("请输入正确的文章列表地址");
            //    return;
            //}
            string strUrl = "https://mp.weixin.qq.com/mp/profile_ext?action=getmsg&__biz=MzU3NjgzNTg2Ng==&f=json&offset=1&count=10&is_ok=1&scene=124&uin=OTI4MzMyMzU%3D&key=c945653c067552139c5cfd908e5da126b943356b9fc6f42405137208ef28211aa85906611fb77ad2eebda0bf96f0c04a9b6774bbf5044aad60bfb87c46df58afad2bf45bf16aff62fa3333a4ae9463903f9330e54b540ce504d181483e694e6cb3bb1d3f798224fb3bb78eaf70cb3f075a7d88749115817c9e2b92eb0056d80d&pass_ticket=M%2FsQygKGWEywfoUWiIbUfdSSexS9WgtW1Lio7dGP5VmPGJf3vtjoVxG2%2FdBeOAVM&wxtoken=&appmsg_token=1183_GLOrmuuOlVUUp%252FpoPwhI2w09stB3cUJm8HW4kg~~&x5=0&f=json";

            request(strUrl);
            Console.WriteLine("导出结束");

        }

        static void request(string strUrl)
        {

            var client = new RestClient(strUrl);
            //client. = -1;
            var request = new RestRequest(new Uri(strUrl), Method.Get);
            var response = client.Execute(request);
            if (response.Content == null)
            {
                Console.WriteLine("该地址无返回列表数据");
                return;
            }
            var jsonResult = JsonConvert.DeserializeObject<jsonModel>(response.Content);
            if (jsonResult?.general_msg_list == null)
            {
                Console.WriteLine("文章列表为空");
                return;
            }
            Console.WriteLine("获取文章列表成功:" + jsonResult.general_msg_list.Count());
            var jsonList = JsonConvert.DeserializeObject<general_msg_Model>(jsonResult.general_msg_list);
            for (int i = 0; i < jsonList?.list?.Count; i++)
            {
                var listmodel = jsonList?.list?[i];
                Console.WriteLine("大标题名称：" + listmodel?.app_msg_ext_info?.title);
                var multiList = listmodel?.app_msg_ext_info?.multi_app_msg_item_list;
                for (int j = 0; j < multiList?.Count; j++)
                {
                    totalCount++;
                    //Console.WriteLine("开始导出文章");
                    var url = multiList?[j].content_url;

                    var pathCurrent = Environment.CurrentDirectory + "/writeFiles/";

                    if (!Directory.Exists(Path.GetDirectoryName(pathCurrent)))
                    {
                        //创建路径
                        Directory.CreateDirectory(Path.GetDirectoryName(pathCurrent));
                    }
                    pathCurrent += "/" + multiList?[j].title + ".html";
                    if (File.Exists(pathCurrent))
                    {
                        Console.WriteLine("第【" + totalCount + "】篇跳过已存在-- 小标题【" + (j + 1) + "】篇：" + multiList?[j].title);
                        continue;
                        //File.Delete(pathCurrent);
                    }

                    var strHtml = WebHandler.GetHtmlStr(url, "utf-8");
                    File.AppendAllText(pathCurrent, strHtml);

                    Console.WriteLine("第【" + totalCount + "】篇-- 小标题【" + (j + 1) + "】篇：" + multiList?[j].title);

                    //Document document = new Spire.Doc.Document();
                    //document.LoadFromFile(pathCurrent);
                    ////document.LoadHTML(strReader, Spire.Doc.Documents.XHTMLValidationType.None);
                    //document.SaveToFile("toDoc.doc", FileFormat.Docx2013);
                }
            }



            var paramList = strUrl.Split("&");

            var strOffset = paramList[3];
            var strPage = strOffset.Split("=").Last();
            var newPage = Convert.ToInt32(strPage) + 10;
            var newOffet = "&" + strOffset.Split("=").First() + "=" + newPage + "&";

            var newUrl = strUrl.Replace("&" + strOffset + "&", newOffet);
            Program.request(newUrl);
        }
    }
}

